# import requests
import random

import requests

from doubanbook.mylib.agent import user_agent_list, referer_list, testurl_list

header = {
    'User-Agent': random.choice(user_agent_list),
    'Referer': random.choice(referer_list)
}


def iptest(IP, timeout):
    # if False == False:
    #     return True
    # try:
    # requests.adapters.DEFAULT_RETRIES = 3
    # IP = random.choice(IPAgents)
    proxy = {
        'http': 'http://' + IP,
        'https': 'https://' + IP,
    }
    # thisProxy = "http://" + IP
    thisIP = "".join(IP.split(":")[0:1])
    print(thisIP)
    # res = requests.get(url="http://icanhazip.com/", headers=header, timeout=timeout, proxies=proxy)
    # proxyIP = res.text
    # if (proxyIP == thisIP):
    #     print("代理IP:'" + proxyIP + "'有效！")
    #     return True
    # else:
    #     # print(proxyIP)
    #     # print("代理IP无效！")
    #     pass
    #     return False
    try:
        req = requests.get(url=random.choice(testurl_list), headers=header, timeout=timeout, proxies=proxy)
    except:

        print('invalid ip and port')
        return False
    else:
        if 200 <= req.status_code < 300:
            print(req.text)
            return True
        else:
            print('invalid ip and port')
            return False


if __name__ == '__main__':
    # getdaili()
    iptest('183.129.244.16:12585', 3)
